package com.agileboot.medicine.controller;


import com.agileboot.common.core.dto.ResponseDTO;
import com.agileboot.common.exception.ApiException;
import com.agileboot.common.exception.error.ErrorCode;
import com.agileboot.common.utils.StringTool;
import com.agileboot.domain.feedback.dto.UserFeedbackReq;
import com.agileboot.domain.search.db.SearchHistory;
import com.agileboot.domain.search.db.SearchHistoryService;
import com.agileboot.domain.search.dto.SearchHistoryDTO;
import com.agileboot.domain.system.user.db.SysUserEntity;
import com.agileboot.domain.system.user.db.SysUserService;
import com.agileboot.infrastructure.user.AuthenticationUtils;
import com.agileboot.infrastructure.user.web.SystemLoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.util.Asserts;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


/**
 * (SearchHistory)表控制层
 *
 * @author makejava
 * @since 2023-12-12 11:12:52
 */
@RestController
@RequestMapping("/searchHistory")
public class SearchHistoryController {

    @Resource
    private SearchHistoryService searchHistoryService;

    @PostMapping("/save")
    @ApiOperation(value = "添加搜索记录", notes = "添加搜索记录")
    public ResponseDTO saveSearchHistory(@RequestBody SearchHistoryDTO searchHistoryDTO) {
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        Asserts.notNull(loginUser,"该用户没有登录");
        Long userId = loginUser.getUserId();
        String key = searchHistoryDTO.getSearchWord();
        if (StringUtils.isBlank(key) || StringUtils.isBlank(String.valueOf(userId))){
            throw new ApiException(ErrorCode.Business.REQUEST_FIELD_EMPTY);
        }
        SearchHistory searchHistory = new SearchHistory();
        BeanUtils.copyProperties(searchHistoryDTO,searchHistory);
        searchHistoryService.save(searchHistory);
        return ResponseDTO.ok("保存成功");
    }

    @GetMapping("/dict")
    @ApiOperation(value = "获取搜索关键词", notes = "获取搜索关键词")
    public ResponseDTO getSearchKey(Long userId) {
        if (StringUtils.isBlank(String.valueOf(userId))){
            throw new ApiException(ErrorCode.Business.REQUEST_FIELD_EMPTY);
        }
        SystemLoginUser loginUser = AuthenticationUtils.getSystemLoginUser();
        Asserts.notNull(loginUser,"该用户没有登录");
        userId = loginUser.getUserId();
        List<String> searchHistoryPage = searchHistoryService.getSearchKey(userId);
        return ResponseDTO.ok(searchHistoryPage);
    }

    @PostMapping("/del")
    @ApiOperation(value = "清空搜索记录", notes = "清空搜索记录")
    public ResponseDTO delSearchKey(@RequestBody SearchHistoryDTO searchHistoryDTO) {
        if (StringUtils.isBlank(searchHistoryDTO.getIds())){
            throw new ApiException(ErrorCode.Business.REQUEST_FIELD_EMPTY);
        }
        List<Long> list = StringTool.toLongList(searchHistoryDTO.getIds());
        searchHistoryService.removeBatchByIds(list);
        return ResponseDTO.ok();
    }

    @Operation(summary = "本月热搜")
    @GetMapping("/hot-search")
    public ResponseDTO<List<String>> getHotSearchMedicineList() throws IOException {
        return ResponseDTO.ok(searchHistoryService.getHotSearchMedicineList());
    }

}

